#include <iostream>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <cstdlib>
using namespace std;

// 阻塞队列
class BlockQueue
{
private:
    queue<int> q;
    mutex mtx;                    // 互斥锁
    condition_variable not_full;  // 队列不满
    condition_variable not_empty; // 队列不空
    size_t cap;                   // 队列容量

public:
    BlockQueue(size_t c = 10) : cap(c) {}

    void enqueue(int val)
    {
        unique_lock<mutex> lock(mtx);
        not_full.wait(lock, [this] { return q.size() < cap; });
        q.push(val);
        cout<<"生产："<<val<<endl;
        not_empty.notify_one();
    }

    int dequeue()
    {
        unique_lock<mutex> lock(mtx);
        not_empty.wait(lock, [this] { return q.size() > 0; });
        int val = q.front();
        q.pop();
        cout<<"消费："<<val<<endl;
        not_full.notify_one();
        return val;
    }
};